﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class CycleCountRuleDA : ICycleCountRuleDA
    {
        private string CC_ADJ_RULE_ID = "@CC_ADJ_RULE_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string NEG_ADJ_EXPIRY_DATE = "@NEG_ADJ_EXPIRY_DATE";
        private string NEG_ADJ_LOT = "@NEG_ADJ_LOT";
        private string NEG_ADJ_PRODUCE_DATE = "@NEG_ADJ_PRODUCE_DATE";
        private string NEG_ADJ_RECEIPT_DATE = "@NEG_ADJ_RECEIPT_DATE";
        private string POS_ADJ_EXPIRY_DATE = "@POS_ADJ_EXPIRY_DATE";
        private string POS_ADJ_LOT = "@POS_ADJ_LOT";
        private string POS_ADJ_PRODUCE_DATE = "@POS_ADJ_PRODUCE_DATE";
        private string POS_ADJ_RECEIPT_DATE = "@POS_ADJ_RECEIPT_DATE";
        private string QTY_GTR_ZERO = "@QTY_GTR_ZERO";
        private string SQL_CHECK_CYCLE_COUNT_RULE_ID_UNIQUE = " SELECT COUNT(1) FROM @_@CYCLE_COUNT_RULE WHERE CC_ADJ_RULE_ID = @CC_ADJ_RULE_ID ";
        private string SQL_DELETE_CYCLE_COUNT_RULE = " DELETE FROM @_@CYCLE_COUNT_RULE WHERE CC_ADJ_RULE_ID = @CC_ADJ_RULE_ID ";
        private string SQL_INSERT_CYCLE_COUNT_RULE = " INSERT INTO @_@CYCLE_COUNT_RULE ( WH_ID, CC_ADJ_RULE_ID, DESCR, POS_ADJ_LOT, POS_ADJ_PRODUCE_DATE, POS_ADJ_EXPIRY_DATE, POS_ADJ_RECEIPT_DATE, QTY_GTR_ZERO, NEG_ADJ_LOT, NEG_ADJ_PRODUCE_DATE, NEG_ADJ_EXPIRY_DATE, NEG_ADJ_RECEIPT_DATE, IS_ACTIVE, SYSTEM_CREATED, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @CC_ADJ_RULE_ID, @DESCR, @POS_ADJ_LOT, @POS_ADJ_PRODUCE_DATE, @POS_ADJ_EXPIRY_DATE, @POS_ADJ_RECEIPT_DATE, @QTY_GTR_ZERO, @NEG_ADJ_LOT, @NEG_ADJ_PRODUCE_DATE, @NEG_ADJ_EXPIRY_DATE, @NEG_ADJ_RECEIPT_DATE, @IS_ACTIVE, @SYSTEM_CREATED, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_CYCLE_COUNT_RULE = " SELECT  ROWID, WH_ID, CC_ADJ_RULE_ID, DESCR, POS_ADJ_LOT, POS_ADJ_PRODUCE_DATE, POS_ADJ_EXPIRY_DATE, POS_ADJ_RECEIPT_DATE, QTY_GTR_ZERO, NEG_ADJ_LOT, NEG_ADJ_PRODUCE_DATE, NEG_ADJ_EXPIRY_DATE, NEG_ADJ_RECEIPT_DATE, IS_ACTIVE, SYSTEM_CREATED, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@CYCLE_COUNT_RULE ";
        private string SQL_UPDATE_CYCLE_COUNT_RULE = " UPDATE @_@CYCLE_COUNT_RULE SET WH_ID = @WH_ID, DESCR = @DESCR, POS_ADJ_LOT = @POS_ADJ_LOT, POS_ADJ_PRODUCE_DATE = @POS_ADJ_PRODUCE_DATE, POS_ADJ_EXPIRY_DATE = @POS_ADJ_EXPIRY_DATE, POS_ADJ_RECEIPT_DATE = @POS_ADJ_RECEIPT_DATE, QTY_GTR_ZERO = @QTY_GTR_ZERO, NEG_ADJ_LOT = @NEG_ADJ_LOT, NEG_ADJ_PRODUCE_DATE = @NEG_ADJ_PRODUCE_DATE, NEG_ADJ_EXPIRY_DATE = @NEG_ADJ_EXPIRY_DATE, NEG_ADJ_RECEIPT_DATE = @NEG_ADJ_RECEIPT_DATE, IS_ACTIVE = @IS_ACTIVE, SYSTEM_CREATED = @SYSTEM_CREATED, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE CC_ADJ_RULE_ID = @CC_ADJ_RULE_ID  ";
        private string SYSTEM_CREATED = "@SYSTEM_CREATED";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckCycleCountRuleIDUnique(string cycleCountRuleID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_ADJ_RULE_ID", cycleCountRuleID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_CYCLE_COUNT_RULE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckCycleCountRuleIDUnique(DataBase dataBase, DbTransaction tran, string cycleCountRuleID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_ADJ_RULE_ID", cycleCountRuleID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_CYCLE_COUNT_RULE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteCycleCountRule(List<string> cycleCountRuleID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < cycleCountRuleID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_ADJ_RULE_ID", cycleCountRuleID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CYCLE_COUNT_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteCycleCountRule(DataBase dataBase, DbTransaction tran, List<string> cycleCountRuleID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < cycleCountRuleID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_ADJ_RULE_ID", cycleCountRuleID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CYCLE_COUNT_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllCycleCountRule(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_RULE.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllCycleCountRule(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_RULE.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public CycleCountRuleInfo GetCycleCountRuleByID(string cyclecountruleID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_RULE.Replace("@_@", whLoginID) + " WHERE CC_ADJ_RULE_ID = @CC_ADJ_RULE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_ADJ_RULE_ID", cyclecountruleID) };
            CycleCountRuleInfo cycleCountRuleInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    cycleCountRuleInfo = new CycleCountRuleInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CC_ADJ_RULE_ID"].ToString(), reader["DESCR"].ToString(), reader["POS_ADJ_LOT"].ToString(), reader["POS_ADJ_PRODUCE_DATE"].ToString(), reader["POS_ADJ_EXPIRY_DATE"].ToString(), reader["POS_ADJ_RECEIPT_DATE"].ToString(), reader["QTY_GTR_ZERO"].ToString(), reader["NEG_ADJ_LOT"].ToString(), reader["NEG_ADJ_PRODUCE_DATE"].ToString(), reader["NEG_ADJ_EXPIRY_DATE"].ToString(), reader["NEG_ADJ_RECEIPT_DATE"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return cycleCountRuleInfo;
        }

        public CycleCountRuleInfo GetCycleCountRuleByID(DataBase dataBase, DbTransaction tran, string cyclecountruleID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_RULE.Replace("@_@", whLoginID) + " WHERE CC_ADJ_RULE_ID = @CC_ADJ_RULE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_ADJ_RULE_ID", cyclecountruleID) };
            CycleCountRuleInfo cycleCountRuleInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                cycleCountRuleInfo = new CycleCountRuleInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CC_ADJ_RULE_ID"].ToString(), reader["DESCR"].ToString(), reader["POS_ADJ_LOT"].ToString(), reader["POS_ADJ_PRODUCE_DATE"].ToString(), reader["POS_ADJ_EXPIRY_DATE"].ToString(), reader["POS_ADJ_RECEIPT_DATE"].ToString(), reader["QTY_GTR_ZERO"].ToString(), reader["NEG_ADJ_LOT"].ToString(), reader["NEG_ADJ_PRODUCE_DATE"].ToString(), reader["NEG_ADJ_EXPIRY_DATE"].ToString(), reader["NEG_ADJ_RECEIPT_DATE"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return cycleCountRuleInfo;
        }

        public DataSet GetCycleCountRuleByQueryList(List<string> sqlWhere, CycleCountRuleQueryEntity cyclecountruleQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_CYCLE_COUNT_RULE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!cyclecountruleQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, cyclecountruleQuery.CurrentPage, cyclecountruleQuery.PageSize, cyclecountruleQuery.SortField, cyclecountruleQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetCycleCountRuleByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, CycleCountRuleQueryEntity cyclecountruleQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_CYCLE_COUNT_RULE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!cyclecountruleQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, cyclecountruleQuery.CurrentPage, cyclecountruleQuery.PageSize, cyclecountruleQuery.SortField, cyclecountruleQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertCycleCountRule(CycleCountRuleInfo cycleCountRuleInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    cycleCountRuleInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CycleCountRule_Parameters(cycleCountRuleInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CYCLE_COUNT_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertCycleCountRule(DataBase dataBase, DbTransaction tran, CycleCountRuleInfo cycleCountRuleInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    cycleCountRuleInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CycleCountRule_Parameters(cycleCountRuleInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CYCLE_COUNT_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_CycleCountRule_Parameters(CycleCountRuleInfo cycleCountRuleInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.CC_ADJ_RULE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.DESCR, SqlDbType.VarChar, 50), new SqlParameter(this.POS_ADJ_LOT, SqlDbType.VarChar, 20), new SqlParameter(this.POS_ADJ_PRODUCE_DATE, SqlDbType.VarChar, 20), new SqlParameter(this.POS_ADJ_EXPIRY_DATE, SqlDbType.VarChar, 20), new SqlParameter(this.POS_ADJ_RECEIPT_DATE, SqlDbType.VarChar, 20), new SqlParameter(this.QTY_GTR_ZERO, SqlDbType.VarChar, 1), new SqlParameter(this.NEG_ADJ_LOT, SqlDbType.VarChar, 20), new SqlParameter(this.NEG_ADJ_PRODUCE_DATE, SqlDbType.VarChar, 20), new SqlParameter(this.NEG_ADJ_EXPIRY_DATE, SqlDbType.VarChar, 20), new SqlParameter(this.NEG_ADJ_RECEIPT_DATE, SqlDbType.VarChar, 20), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.SYSTEM_CREATED, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(cycleCountRuleInfo.WhID))
            {
                paramArray[0].Value = cycleCountRuleInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = cycleCountRuleInfo.CcAdjRuleID;
            paramArray[2].Value = cycleCountRuleInfo.Descr;
            paramArray[3].Value = cycleCountRuleInfo.PosAdjLot;
            paramArray[4].Value = cycleCountRuleInfo.PosAdjProduceDate;
            paramArray[5].Value = cycleCountRuleInfo.PosAdjExpiryDate;
            paramArray[6].Value = cycleCountRuleInfo.PosAdjReceiptDate;
            paramArray[7].Value = cycleCountRuleInfo.QtyGtrZero;
            paramArray[8].Value = cycleCountRuleInfo.NegAdjLot;
            paramArray[9].Value = cycleCountRuleInfo.NegAdjProduceDate;
            paramArray[10].Value = cycleCountRuleInfo.NegAdjExpiryDate;
            paramArray[11].Value = cycleCountRuleInfo.NegAdjReceiptDate;
            paramArray[12].Value = cycleCountRuleInfo.IsActive;
            paramArray[13].Value = cycleCountRuleInfo.SystemCreated;
            paramArray[14].Value = cycleCountRuleInfo.CreatedBy;
            paramArray[15].Value = cycleCountRuleInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateCycleCountRule(CycleCountRuleInfo cycleCountRuleInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    cycleCountRuleInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CycleCountRule_Parameters(cycleCountRuleInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CYCLE_COUNT_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateCycleCountRule(DataBase dataBase, DbTransaction tran, CycleCountRuleInfo cycleCountRuleInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    cycleCountRuleInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CycleCountRule_Parameters(cycleCountRuleInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CYCLE_COUNT_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

